package com.qin.entity;

import com.qin.controller.FunctionController;
import com.qin.entity.components.DatabaseComponent;
import com.qin.entity.components.FunctionComponent;
import com.qin.entity.components.InterfaceComponent;
import com.qin.entity.components.LogComponent;
import com.qin.service.ComponentService;
import com.qin.service.ExecuteService;
import com.qin.service.impl.ComponentServiceImpl;
import com.qin.service.impl.ExecuteServiceImpl;
import lombok.Data;
import java.util.List;

@Data
public class Component {

    //主键
    private String component_id;
    //所属案例ID
    private String testcase_id;
    //序号
    private int order_number;
    //组件名称
    private String component_name;
    //组件类型（0-接口组件，1-数据库组件,2-IF组件，3-函数组件，4-日志组件）
    private int component_type;
    //是否执行（0-否，1-是）
    private int is_used;

    //【接口/数据库组件】操作的系统
    private String system_name;
    //【接口组件】接口ID
    private String interface_id;
    //【接口组件】接口入参
    private String input_json;
    //【数据库组件】输入SQL
    private String input_sql;
    //【IF组件】输入条件
    private String input_if;
    //【日志组件】输出日志
    private String output_log;
    //子组件列表
    private List<SubComponent> subComponentList;

    //是否执行子组件标志位（true-执行，false-不执行）
    private boolean exec_subCom_flag = true;
    //是否打印日志（true-打印，false-不打印）
    private boolean print_log_flag = true;

    private ExecuteService executeService = new ExecuteServiceImpl();

    public void execute(ExecuteBean executeBean){
        //补全本Component对象的各属性
        this.complete();
        //设置ExecuteBean对象的各属性
        executeBean.setComponent_id(component_id);
        //设置ExecuteLog对象的各属性
        ExecuteLog executeLog = new ExecuteLog(executeBean.getExecute_id(), executeBean.getFunction_id(),
                executeBean.getTestcase_id(), executeBean.getComponent_id(), "", "");
        //执行当前组件内容
        if(print_log_flag){
            String component_type_name = null;
            switch (component_type){
                case 0: component_type_name = "接口组件"; break;
                case 1: component_type_name = "数据库组件"; break;
                case 2: component_type_name = "IF组件"; break;
                case 3: component_type_name = "函数组件"; break;
                case 4: component_type_name = "日志组件"; break;
            }
            //插入日志
            executeService.insertExecuteLog(executeLog, ">>>>>>>>>>>>>>>>>>>>" + component_type_name + "【" + component_name + "】执行开始");
        }

        //0-接口组件
        if(this.getComponent_type() == 0){
            InterfaceComponent interfaceComponent = new InterfaceComponent(component_id, system_name, interface_id, input_json, subComponentList);
            interfaceComponent.execute(executeBean);
        }
        //1-数据库组件
        else if(this.getComponent_type() == 1){
            DatabaseComponent databaseComponent = new DatabaseComponent(component_id, system_name, input_sql, subComponentList, exec_subCom_flag, print_log_flag);
            databaseComponent.execute(executeBean);
        }
        //2-IF组件
        else if(this.getComponent_type() == 2){
//            System.out.println("Component_type: " + component_type);
//            System.out.println(input_if);

        }
        //3-函数组件
        else if(this.getComponent_type() == 3){
            FunctionComponent functionComponent = new FunctionComponent(subComponentList);
            functionComponent.execute(executeBean);
        }
        //4-日志组件
        else if(this.getComponent_type() == 4){
            LogComponent logComponent = new LogComponent(output_log);
            logComponent.execute(executeBean);
        }

    }


    public void complete(){
        //补全本Component对象的各属性
        ComponentService componentService = new ComponentServiceImpl();
        Component component = componentService.queryComponentByComponentId(this);

        this.setComponent_id(component.component_id);
        this.setTestcase_id(component.testcase_id);
        this.setOrder_number(component.order_number);
        this.setComponent_name(component.component_name);
        this.setComponent_type(component.component_type);
        this.setIs_used(component.is_used);

        this.setSystem_name(component.system_name);
        this.setInterface_id(component.interface_id);
        this.setInput_json(component.input_json);
        this.setInput_sql(component.input_sql);
        this.setInput_if(component.input_if);
        this.setOutput_log(component.output_log);

        this.setSubComponentList(component.getSubComponentList());
    }


}
